/**
 * Created by zhaoxk on 2017/2/24.
 */
import React from 'react';
import { connect } from 'dva';
import _ from 'lodash';
import Table from '../../../components/table';
import InlineList from '../../../components/inline-list';

function Cost({ data, dispatch, patient: { tableShow } }) {
    const cols = [
        {
            title: '项目',
            dataIndex: 'title',
        }, {
            title: '总金额',
            dataIndex: 'total',
        }, {
            title: '自费金额',
            dataIndex: 'pay',
        },
    ];

    /*
     * 从业务层变更当前展开的tbody项;
     * 这有些违背'顶层容器型组件直接绑定state'的原则，直接将state绑定至展示组件上；
     * 虽然功能可以实现，但是不利于统一管理state，和代码阅读，由但是省去了将回调绑定至容器型组件逐级下传的麻烦；
     */
    const onPress = (tableType) => (index) => {
        dispatch({
            type: 'patient/toggleTableShow',
            payload: {
                tableType,
                index,
            }
        });
    };
    const extra = (data) => {
        const { code, detail } = data;
        const result = _.map(detail, (d, i) => {
            return {
                code: `${code}${i}`,
                label: d.name,
                value: d.price,
            };
        });
        if (detail && detail.length > 0) {
            return (
                <div>
                    <InlineList
                        data={result}
                        labelWidth="5rem"
                    />
                </div>
            );
        } else {
            return (
                <div
                    style={{
                        padding: '.5rem 0',
                        textAlign: 'center',
                        color: '#999'
                    }}
                >
                    暂无数据
                </div>
            );
        }
    };
    return (
        <Table
            data={data}
            cols={cols}
            extra={extra}
            onPress={onPress.bind(null, 'cost')()}
            openIndex={tableShow.cost}
        />
    );
}

function mapStateToProps({ patient }) {
    return { patient };
}

export default connect(mapStateToProps)(Cost);
